﻿using DotNetCommon.Extensions;
using NUnit.Framework;
using Shouldly;
using System;
using System.Collections.Generic;
using System.Text;

namespace Test.MySql
{
    [TestFixture]
    internal class ColumnTypesTestInts : TestBase
    {
        #region 模型
        public class Model
        {
            /// <summary>
            /// column: tinyint
            /// </summary>
            public sbyte? t_tinyint { get; set; }
            /// <summary>
            /// column: smallint
            /// </summary>
            public short? t_smallint { get; set; }
            /// <summary>
            /// column: mediumint
            /// </summary>
            public int? t_mediumint { get; set; }
            /// <summary>
            /// column: int
            /// </summary>
            public int? t_int { get; set; }
            /// <summary>
            /// column: bigint
            /// </summary>
            public long? t_bitint { get; set; }
        }
        #endregion

        [SetUp]
        public void Setup()
        {
            db.Manage.DropTableIfExist("test");
            db.ExecuteSql(@"
create table test.test(
	t_bit bit,
	t_bit8 bit(8),
	t_bit16 bit(16),
	t_bit32 bit(32),
	t_bit64 bit(64),
	t_bit4 bit(4)
)");
            db.ExecuteSql("insert into test.test(t_bit,t_bit8,t_bit16,t_bit32,t_bit64,t_bit4)values(true,1,2,3,4,5)");
            db.ExecuteSql("insert into test.test(t_bit,t_bit8,t_bit16)values(0x01,0xFF,0x0001)");
            db.ExecuteSql("insert into test.test(t_bit,t_bit8)values(b'1',b'01010001')");
        }

        [Test]
        public void Test()
        {
            var dic = db.SelectDictionary("select * from test");
            //ado统一使用 ulong解析
            Assert.IsTrue(dic["t_bit"].GetType() == typeof(ulong));
            Assert.IsTrue(dic["t_bit16"].GetType() == typeof(ulong));
            Assert.IsTrue(dic["t_bit16"].GetType() == typeof(ulong));
        }

        [Test]
        public void TestOrmRead()
        {
            //var models = db.SelectModelList<Model>("select * from test");
            //models[0].t_bit.ShouldBe(true);
            //models[0].t_bit8.ShouldBe((byte)0x01);
            //models[0].t_bit16.ShouldBe((ushort)0x0002);
            //models[0].t_bit32.ShouldBe((uint)0x0000_0003);
            //models[0].t_bit64.ShouldBe((ulong)0x0000_0000_0004);
            //models[0].t_bit4.ShouldBe((byte)0x05);
        }

        [Test]
        public void TestOrmWrite()
        {
            //db.Manage.TruncateTable("test");
            //var model = new Model
            //{
            //    t_bit = true,
            //    t_bit8 = (byte)0x01,
            //    t_bit16 = (ushort)0x0002,
            //    t_bit32 = (uint)0x0000_0003,
            //    t_bit64 = (ulong)0x0000_0000_0004,
            //    t_bit4 = (byte)0x05
            //};
            //var res = db.Insert("test", model.ToDictionary());
            //res.ShouldBe(1);
            //var models = db.SelectModelList<Model>("select * from test");

            //models[0].t_bit.ShouldBe(true);
            //models[0].t_bit8.ShouldBe((byte)0x01);
            //models[0].t_bit16.ShouldBe((ushort)0x0002);
            //models[0].t_bit32.ShouldBe((uint)0x0000_0003);
            //models[0].t_bit64.ShouldBe((ulong)0x0000_0000_0004);
            //models[0].t_bit4.ShouldBe((byte)0x05);
        }
    }
}
